<template>
  <div class="container">
    <van-nav-bar fixed title="登录" @click-left="$router.back()" left-arrow />

    <div class="logo-txt">优点达资讯</div>
    <van-form @submit="onSubmit" label-width="50px">
      <van-field
        v-model="mobile"
        name="mobile"
        label="用户名"
        placeholder="请输入手机号"
        left-icon="manager"
        :rules="[
          { required: true, message: '请填写用户名' },
          {
            pattern: /^(?:(?:\+|00)86)?1[3-9]\d{9}$/,
            message: '手机号格式不正确！',
          },
        ]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        left-icon="lock"
        :rules="[
          { required: true, message: '请填写密码' },
          {
            pattern: /^\w{6,12}$/,
            message: '密码应为6~12位字符',
          },
        ]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
    <van-row type="flex" justify="end">
      <van-col :span="10">
        <router-link to="/reg">没有账号,立即注册</router-link>
      </van-col>
    </van-row>
  </div>
</template>

<script>
export default {
  data () {
    return {
      mobile: '',
      password: '',
    }
  },
  methods: {
    async onSubmit (values) {
      try {
        await this.$store.dispatch('user/loginAction', values)
        this.$toast.success('登录成功')
        this.$router.replace('/user')
        localStorage.setItem('USER_INFO', JSON.stringify(values))
      } catch (error) {
       return
      }

    },
  },
}
</script>

<style scoped>
.logo-txt {
  text-align: center;
  font-size: 60px;
  margin: 60px auto;
  color: #1989fa;
}
</style>
